package com.scau.service.Impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.scau.dao.Impl.StudentDaoImpl;
import com.scau.pojo.Student;
import com.scau.service.StudentService;

@Service
public class StudentServiceImpl implements StudentService{
	@Autowired
	StudentDaoImpl studentDAO;

	@Override
	public List<Student> query() {
		return studentDAO.query();
	}

	@Override
	public int add(JSONObject jsonObject) {
		//Gson gson=new Gson();
		//Student student=gson.fromJson(jsonObject.toJSONString(), Student.class);
		Student student=new Student();
		String stName=jsonObject.getString("name");
		String stNickname=jsonObject.getString("nickname");
		String stPassword=jsonObject.getString("password");
		String stSex=jsonObject.getString("sex");
		String stTel=jsonObject.getString("tel");
		String stSchool=jsonObject.getString("school");
		String stMajor=jsonObject.getString("major");
		String stGrade=jsonObject.getString("grade");
		student.setStGrade(stGrade);
		student.setStMajor(stMajor);
		student.setStName(stName);
		student.setStNickname(stNickname);
		student.setStPassword(stPassword);
		student.setStSchool(stSchool);
		student.setStSex(stSex);
		student.setStTel(stTel);
		student.setStValidation(1);
		return studentDAO.add(student);
	}

	@Override
	public int delete(int stId) {
		return studentDAO.delete(stId);
	}

	@Override
	public int deleteMulti(JSONArray jsonArray){
		int result=0;
		List<Integer> idArr=JSON.parseArray(jsonArray.toString(), Integer.class);
		try {
			for(int id : idArr){
				if(studentDAO.delete(id)!=1){
					break;
					
				}else{
					result=1;
				}
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return result;
	}

	@Override
	public int update(JSONObject jsonObject) {
		int result=0;
		Student student=new Student();
		int stId=jsonObject.getInteger("id");
		String stName=jsonObject.getString("name");
		String stNickname=jsonObject.getString("nickname");
		String stPassword=jsonObject.getString("password");
		String stSex=jsonObject.getString("sex");
		String stTel=jsonObject.getString("tel");
		String stSchool=jsonObject.getString("school");
		String stMajor=jsonObject.getString("major");
		String stGrade=jsonObject.getString("grade");
		student.setStId(stId);
		student.setStGrade(stGrade);
		student.setStMajor(stMajor);
		student.setStName(stName);
		student.setStNickname(stNickname);
		student.setStPassword(stPassword);
		student.setStSchool(stSchool);
		student.setStSex(stSex);
		student.setStTel(stTel);
		try {
			result=studentDAO.update(student);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return result;
	}
	
	@Override
	public int updatePwd(JSONObject jsonObject) {
		int result=0;
		Student student=new Student();
		int stId=jsonObject.getInteger("id");
		String stPassword=jsonObject.getString("password");
		student.setStId(stId);
		student.setStPassword(stPassword);
		try {
			result=studentDAO.update(student);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return result;
	}

	
}
